package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        ch:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用！");
                    break ch;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }

    }

    //修改老师
    private void updateTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息，请添加后再修改");
            return;
        }
        String id = inputTeacherId();
        Teacher ter = inputTeacherInfo(id);
        teacherService.updateTeacher(id, ter);


    }

    //删除老师
    private void deleteTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息，请添加后再删除");
            return;
        }
        String id = inputTeacherId();
        teacherService.deleteTeacher(id);
        System.out.println("删除成功");


    }

    //查看老师
    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息，请添加后再查看");
            return;
        }
        System.out.println("学号\t\t姓名 \t年龄 \t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher tea = teachers[i];
            if (tea != null) {
                System.out.println(tea.getId() + " \t" + tea.getName() + " \t" + tea.getAge() + " \t" + tea.getBirthday());
            }
        }
    }

    //添加老师
    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入ID");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("学号已被占用，请重新输入");
            } else {
                break;
            }
        }
        Teacher tea = inputTeacherInfo(id);
        boolean result = teacherService.addTeacher(tea);
        if (result) {
            System.out.println("添加完成");
        } else {
            System.out.println("添加失败");
        }
    }

    public String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入您要修改的ID");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在,重新输入");
            } else {
                break;
            }
        }
        return id;
    }

    //封装老师对象
    public Teacher inputTeacherInfo(String id) {
        System.out.println("请输入名字");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birshtey = sc.next();
        Teacher ter = new Teacher(id, name, age, birshtey);
        return ter;
    }
}
